﻿using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.IO;
using System.Reflection;

namespace Chengqbd.Core.Extensions
{
	public static class Extensions
	{
		public static bool IsNullOrEmpty(this string value)
		{
			return string.IsNullOrEmpty(value);
		}
        
		public static bool IsNullOrWhiteSpace(this string value)
		{
			return string.IsNullOrWhiteSpace(value);
		}
        
		public static string ToBarStr(this Guid guid)
		{
			return guid.ToString();
		}
        
		public static string ToNotBarStr(this Guid guid)
		{
			return guid.ToString("N");
		}
        /// <summary>
        /// 返回ENUM名
        /// </summary>
        /// <param name="en"></param>
        /// <returns></returns>
		public static string Display(this Enum en)
		{
			Type type = en.GetType();
			string key = type.ToString() + "." + en.ToString();
			bool flag = !enumDisplayValueDict.ContainsKey(key);
			if (flag)
			{
				bool flag2 = en.ToString().IsNum();
				if (flag2)
				{
                    enumDisplayValueDict.TryAdd(key, en.ToString());
				}
				else
				{
					DisplayAttribute customAttribute = type.GetField(en.ToString()).GetCustomAttribute<DisplayAttribute>();
					bool flag3 = customAttribute != null;
					if (flag3)
					{
						bool flag4 = !customAttribute.Name.IsNullOrEmpty();
						if (flag4)
						{
                            enumDisplayValueDict.TryAdd(key, customAttribute.Name);
						}
						else
						{
							bool flag5 = !customAttribute.Description.IsNullOrEmpty();
							if (flag5)
							{
                                enumDisplayValueDict.TryAdd(key, customAttribute.Description);
							}
							else
							{
                                enumDisplayValueDict.TryAdd(key, en.ToString());
							}
						}
					}
					else
					{
                        enumDisplayValueDict.TryAdd(key, en.ToString());
					}
				}
			}
			return enumDisplayValueDict[key];
		}
        
		public static int Value(this Enum en)
		{
			return Convert.ToInt32(en);
		}
        /// <summary>
        /// 返回枚举所有item
        /// </summary>
        /// <param name="enumType"></param>
        /// <returns></returns>
		public static List<EnumItem> GetItems(this Type enumType)
		{
			bool flag = !enumType.IsEnum;
			if (flag)
			{
				throw new InvalidCastException();
			}
			List<EnumItem> list = new List<EnumItem>();
			foreach (FieldInfo fieldInfo in enumType.GetFields())
			{
				bool isEnum = fieldInfo.FieldType.IsEnum;
				if (isEnum)
				{
					int num = (int)enumType.InvokeMember(fieldInfo.Name, BindingFlags.GetField, null, null, null);
					bool flag2 = num > 0;
					if (flag2)
					{
						string name = string.Empty;
						object[] customAttributes = fieldInfo.GetCustomAttributes(typeof(DescriptionAttribute), false);
						bool flag3 = customAttributes.Length != 0;
						if (flag3)
						{
							name = ((DescriptionAttribute)customAttributes[0]).Description;
						}
						else
						{
							name = fieldInfo.Name;
						}
						list.Add(new EnumItem
						{
							Id = num,
							Name = name
						});
					}
				}
			}
			return list;
		}
        /// <summary>
        /// 返回枚举所有item
        /// </summary>
        /// <param name="enumType"></param>
        /// <returns></returns>
		public static IEnumerable<KeyValuePair<string, int>> ToList(this Enum en)
		{
			Type type = en.GetType();
			bool flag = !Extensions.enumToListDict.ContainsKey(type);
			if (flag)
			{
				FieldInfo[] fields = type.GetFields(BindingFlags.Static | BindingFlags.Public);
				List<KeyValuePair<string, int>> list = new List<KeyValuePair<string, int>>(fields.Length);
				FieldInfo[] array = fields;
				foreach (FieldInfo fieldInfo in array)
				{
					DisplayAttribute customAttribute = fieldInfo.GetCustomAttribute<DisplayAttribute>();
					bool flag2 = customAttribute != null;
					if (flag2)
					{
						bool flag3 = !customAttribute.Name.IsNullOrEmpty();
						if (flag3)
						{
							list.Add(new KeyValuePair<string, int>(customAttribute.Name, (int)fieldInfo.GetValue(fieldInfo)));
						}
						else
						{
							bool flag4 = !customAttribute.Description.IsNullOrEmpty();
							if (flag4)
							{
								list.Add(new KeyValuePair<string, int>(customAttribute.Description, (int)fieldInfo.GetValue(fieldInfo)));
							}
							else
							{
								list.Add(new KeyValuePair<string, int>(fieldInfo.Name, (int)fieldInfo.GetValue(fieldInfo)));
							}
						}
					}
					else
					{
						list.Add(new KeyValuePair<string, int>(fieldInfo.Name, (int)fieldInfo.GetValue(fieldInfo)));
					}
				}
                enumToListDict.TryAdd(type, list);
			}
			return enumToListDict[type];
		}
        /// <summary>
        /// 随机生成整数
        /// </summary>
        /// <param name="random"></param>
        /// <returns></returns>
		public static int NextInt(this Random random)
		{
			return random.Next(0, int.MaxValue);
		}
        /// <summary>
        /// 随机生成整数
        /// max数值是上限
        /// </summary>
        /// <param name="random"></param>
        /// <param name="max"></param>
        /// <returns></returns>
		public static int NextInt(this Random random, int max)
		{
			return random.Next(max);
		}
        /// <summary>
        /// 随机整数
        /// min,max是上下限
        /// </summary>
        /// <param name="random"></param>
        /// <param name="min"></param>
        /// <param name="max"></param>
        /// <returns></returns>
		public static int NextInt(this Random random, int min, int max)
		{
			return random.Next(min, max);
		}
        
		public static double NextDouble(this Random random, double max)
		{
			return random.NextDouble() * max;
		}
        
		public static double NextDouble(this Random random, double min, double max)
		{
			return random.NextDouble() * (max - min) + min;
		}
        
		public static double Floor(this double value)
		{
			return Math.Floor(value);
		}
        
		public static decimal Floor(this decimal value)
		{
			return Math.Floor(value);
		}
        
		public static double Ceiling(this double value)
		{
			return Math.Ceiling(value);
		}
        
		public static decimal Ceiling(this decimal value)
		{
			return Math.Ceiling(value);
		}
        
		public static double Round(this double value)
		{
			return Math.Round(value);
		}
        
		public static decimal Round(this decimal value)
		{
			return Math.Round(value);
		}
        
		public static double Round(this double value, int digits)
		{
			return Math.Round(value, digits);
		}
        
		public static decimal Round(this decimal value, int digits)
		{
			return Math.Round(value, digits);
		}
        /// <summary>
        /// 随机验证码
        /// </summary>
        /// <param name="codeCount">长度</param>
        /// <returns></returns>
		public static string GenerateCheckCodeNum(this int codeCount)
		{
			string text = string.Empty;
			long ticks = DateTime.Now.Ticks;
			Random random = new Random((int)(ticks & (long.MaxValue)));
			for (int i = 0; i < codeCount; i++)
			{
				int num = random.Next();
				text += ((char)(48 + (ushort)(num % 10))).ToString();
			}
			return text;
		}
        /// <summary>
        /// 随机验证码
        /// </summary>
        /// <param name="codeCount"></param>
        /// <returns></returns>
		public static string GenerateCheckCode(this int codeCount)
		{
			string text = string.Empty;
			long ticks = DateTime.Now.Ticks;
			Random random = new Random((int)(ticks & (long.MaxValue)));
			for (int i = 0; i < codeCount; i++)
			{
				int num = random.Next();
				bool flag = num % 2 == 0;
				char c;
				if (flag)
				{
					c = (char)(48 + (ushort)(num % 10));
				}
				else
				{
					c = (char)(65 + (ushort)(num % 26));
				}
				text += c.ToString();
			}
			return text;
		}
        /// <summary>
        /// 指定字符的验证码
        /// </summary>
        /// <param name="allChar"></param>
        /// <param name="CodeCount"></param>
        /// <returns></returns>
		public static string GenerateRandomCode(this string allChar, int CodeCount)
		{
			string[] array = allChar.Split(new char[]
			{
				','
			});
			string text = "";
			int num = -1;
			Random random = new Random();
			for (int i = 0; i < CodeCount; i++)
			{
				bool flag = num != -1;
				if (flag)
				{
					random = new Random(num * i * (int)DateTime.Now.Ticks);
				}
				int num2 = random.Next(array.Length - 1);
				while (num == num2)
				{
					num2 = random.Next(array.Length - 1);
				}
				num = num2;
				text += array[num2];
			}
			return text;
		}
    
        
		private static ConcurrentDictionary<string, string> enumDisplayValueDict = new ConcurrentDictionary<string, string>();
        
		private static ConcurrentDictionary<Type, IEnumerable<KeyValuePair<string, int>>> enumToListDict = new ConcurrentDictionary<Type, IEnumerable<KeyValuePair<string, int>>>();
	}
}
